package com.medical.medical.controller.clcontroller;

import com.medical.medical.entity.Drug;
import com.medical.medical.entity.Record;
import com.medical.medical.service.DrugService;
import com.medical.medical.service.RecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

/**
 * @authorDesc 古娜拉黑暗之神!
 * @author ChenLi
 * @date 2022-09-14 08:38:49
 * @version 1.0.0
 * @description 诊断报告请求层
 */
@Controller
@Slf4j
@RequestMapping("record")
public class RecordController {
    @Resource
    private RecordService recordService;
    @Resource
    private DrugService drugService;

    //页面大小
    private static final int PAGE_SIZE = 3;

    /**
      * @description 进入user/userMain页面
      * @author ChenLi
      * @date 2022-09-14 18:04:20
      * @param
      * @return {@link String}
      */
    @RequestMapping("intousermain")
    public String intothird(){
        log.info("进入user/userMain页面");
        return "user/userMain";
    }
    /**
      * @description 查看报告
      * @author ChenLi
      * @date 2022-09-14 13:33:22
      * @param id
     * @param model
      * @return {@link String}
      */
    @RequestMapping("torecord")
    public String getReport(int id,String currentPage, Model model){
        int current = 0;
        int state = 0;
        if (currentPage != null) {
            try {
                current = Integer.valueOf(currentPage);
            } catch (NumberFormatException e) {
                e.printStackTrace();
                log.info("解析异常");
            }
        }
        int allPage = 0;
        int allCount = 0;
        allCount = recordService.getRecordAllCount(id);
        log.info("总条数:{}", allCount);

        //查询总条数为0
        if (allCount==0){
            state = -1;
            return "user/record";
        }

        if (allCount % PAGE_SIZE == 0) {
            allPage = allCount / PAGE_SIZE;
        } else {
            allPage = allCount / PAGE_SIZE + 1;
        }
        if (current < 1) {
            current = 1;
        } else if (current > allPage) {
            current = allPage;
        }
        log.info("当前页:{}",current);
        int start = (current - 1) * PAGE_SIZE;


        List<Record> recordList = null;
        state = recordService.getState(id);
        log.info("用户状态:{}",state);
        if (state==2){
            recordList = recordService.getReport(id,start,PAGE_SIZE);
            log.info("{}",recordList);
        }
        model.addAttribute("recordList",recordList);
        model.addAttribute("state",state);
        model.addAttribute("current", current);
        model.addAttribute("allPage", allPage);
        log.info("{}",state);
        log.info("进入user/record页面");
        return "user/record";
    }

    /**
      * @description 查看详情
      * @author ChenLi
      * @date 2022-09-14 13:32:58
      * @param
      * @return {@link String}
      */
    @RequestMapping("view")
    public String view(long recordOrdinal, int recordId, Model model){
        log.info("就诊编号:{}",recordOrdinal);
        log.info("就诊记录ID:{}",recordId);
        Record record = recordService.viewByRecordOrdinal(recordOrdinal);
        log.info("{}",record);
        model.addAttribute("record",record);
        List<Drug> drugList = drugService.getDrugByRecordId(recordId);
        double drugTotalPrice = 0;
        for (Drug drug : drugList) {
            drugTotalPrice  += drug.getDrugSinglePrice();
        }
        log.info("{}",drugList);
        model.addAttribute("drugList",drugList);
        model.addAttribute("drugTotalPrice",drugTotalPrice);
        log.info("进入user/recordview页面");
        return "user/recordview";
    }
}
